package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.GoodsTypeDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.Log;
import com.atguigu.jxc.service.CustomerReturnListGoodsService;
import com.atguigu.jxc.service.GoodsService;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.SaleListGoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Resource
    private GoodsDao goodsDao;
    @Resource
    private SaleListGoodsService saleListGoodsService;
    @Resource
    private GoodsTypeDao goodsTypeDao;

    @Override
    public ServiceVO getCode() {

        // 获取当前商品最大编码
        String code = goodsDao.getMaxCode();

        // 在现有编码上加1
        Integer intCode = Integer.parseInt(code) + 1;

        // 将编码重新格式化为4位数字符串形式
        String unitCode = intCode.toString();

        for(int i = 4;i > intCode.toString().length();i--){

            unitCode = "0"+unitCode;

        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, unitCode);
    }

    @Override
    public Integer getInventoryTotalCount() {
        return goodsDao.selectInventoryTotalCount();
    }

    @Override
    public PageInfo<Goods> listInventory(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {
        //设置分页信息，该设置需在读取mapper中的方法前执行
        PageHelper.startPage(page, rows);
        //获取所有商品结果集
        Goods tGoods = new Goods();
        try {
            Integer.parseInt(codeOrName);
            tGoods.setGoodsCode(codeOrName);
        } catch (NumberFormatException e) {
            tGoods.setGoodsName(codeOrName);
        }
        tGoods.setGoodsTypeId(goodsTypeId);
        List<Goods> goodsList = goodsDao.selectListInventory(tGoods);

        //为销售总数赋值
        goodsList.forEach(goods -> {
            goods.setSaleTotal(saleListGoodsService.getSaleTotalByGoodsId(goods.getGoodsId()));
        });
        PageInfo<Goods> pageInfo = new PageInfo<>(goodsList);

        return pageInfo;
//        return goodsDao.selectListInventory((page - 1) * rows, rows);
    }

    @Override
    public PageInfo<Goods> getGoodsList(Integer page, Integer rows, String goodsName, Integer goodsTypeId) {
        //设置分页信息，该设置需在读取mapper中的方法前执行
        PageHelper.startPage(page, rows);
        //获取所有商品结果集
        Goods goods = new Goods();
        goods.setGoodsName(goodsName);
        goods.setGoodsTypeId(goodsTypeId);
        List<Goods> goodsList = goodsDao.selectListInventory(goods);

        PageInfo<Goods> pageInfo = new PageInfo<>(goodsList);

        return pageInfo;
    }

    @Override
    public void save(Goods goods) {
        goods.setInventoryQuantity(0);
        goods.setState(0);
        goodsDao.saveGoods(goods);
    }

    @Override
    public void delete(Integer goodsId) {
        final Integer state = 0;
        Goods goods = new Goods();
        goods.setGoodsId(goodsId);
        goods = goodsDao.selectOne(goods);
        if (state.equals(goods.getState())) {
            goodsDao.deleteById(goodsId);
        }
    }

    @Override
    public PageInfo<Goods> getNoInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        PageHelper.startPage(page, rows);
        Goods tGoods = new Goods();
        try {
            Integer.parseInt(nameOrCode);
            tGoods.setGoodsCode(nameOrCode);
        } catch (Exception e) {
            tGoods.setGoodsName(nameOrCode);
        }
        List<Goods> goodsList = goodsDao.selectListInventory(tGoods);
        List<Goods> noInventoryList = goodsList.stream().filter(goods -> goods.getInventoryQuantity().compareTo(0) <= 0).collect(Collectors.toList());
        PageInfo<Goods> pageInfo = new PageInfo<>(noInventoryList);
        return pageInfo;
    }

    @Override
    public PageInfo<Goods> getHasInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        PageHelper.startPage(page, rows);
        Goods tGoods = new Goods();
        try {
            Integer.parseInt(nameOrCode);
            tGoods.setGoodsCode(nameOrCode);
        } catch (Exception e) {
            tGoods.setGoodsName(nameOrCode);
        }
        List<Goods> goodsList = goodsDao.selectListInventory(tGoods);
        List<Goods> noInventoryList = goodsList.stream().filter(goods -> goods.getInventoryQuantity().compareTo(0) > 0).collect(Collectors.toList());
        PageInfo<Goods> pageInfo = new PageInfo<>(noInventoryList);
        return pageInfo;
    }

    @Override
    public void saveStock(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {
        Goods goods = new Goods();
        goods.setGoodsId(goodsId);
        goods.setInventoryQuantity(inventoryQuantity);
        goods.setPurchasingPrice(purchasingPrice);
        goodsDao.updateGoodsById(goods);
    }

    @Override
    public void deleteStock(Integer goodsId) {
        final Integer state = 0;
        Goods goods = new Goods();
        goods.setGoodsId(goodsId);
        goods = goodsDao.selectOne(goods);
        if (null != goods && state.equals(goods.getState())) {
            goods.setInventoryQuantity(0);
            goodsDao.updateGoodsById(goods);
        }
    }

    @Override
    public List<Goods> listAlarm() {

        return goodsDao.selectListAlarm();
    }


}
